package org.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.example.test.UserService;
import org.example.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class testController {
    @GetMapping("/hello")
    @SentinelResource(value = "testController",blockHandler = "handleException")
    public JsonResult  testController1() {
        System.out.println("testController");
        JsonResult jsonResult
                = new JsonResult();
        jsonResult.setMessage("testController1");

        return jsonResult;
    }

    public static JsonResult handleException(BlockException e){
        JsonResult jsonResult = new JsonResult();
        System.out.println("============"+e.getClass());
        if (e instanceof FlowException){
            jsonResult.setMessage("流控异常");
        }else if (e instanceof DegradeException){
            jsonResult.setMessage("降级异常");
        }else if (e instanceof ParamFlowException){
            jsonResult.setMessage("热点异常");
        }else{
            jsonResult.setMessage("其他sentinel异常");
        }
        return jsonResult;
    }
    public static JsonResult handleException1(String id,BlockException e){
        JsonResult jsonResult = new JsonResult();
        System.out.println("============"+e.getClass());
        if (e instanceof FlowException){
            jsonResult.setMessage("流控异常");
        }else if (e instanceof DegradeException){
            jsonResult.setMessage("降级异常");
        }else if (e instanceof ParamFlowException){
            jsonResult.setMessage("热点异常");
        }else{
            jsonResult.setMessage("其他sentinel异常");
        }
        return jsonResult;
    }
    @SentinelResource(value = "testController1")
    @GetMapping("/hello1{id}")
    public JsonResult testController11(@PathVariable("id") String id) throws InterruptedException {
        System.out.println(id);
        //Thread.sleep(8000);
        System.out.println("testController"+id);
        JsonResult jsonResult
                = new JsonResult();
        jsonResult.setMessage("testController1");
        return jsonResult;
    }
    @GetMapping("/add")
    @SentinelResource(value = "add",blockHandler = "handleException")
    public JsonResult add() throws InterruptedException {
        //todo 待开发
        Thread.sleep(3000);
        JsonResult jsonResult= new JsonResult();
        jsonResult.setMessage("testController1");

        return jsonResult;
    }
    @GetMapping("/del")
    @SentinelResource(value = "del",blockHandler = "handleException")
    public  String sel(){
        return "查询订单";
    }
    @Autowired
    UserService userService;

    @GetMapping("/test1")
    @SentinelResource(value = "test1",blockHandler = "handleException")
    public String test1(){
        userService.getUser();
        return "添加订单";
    }
    @GetMapping("/test2")
    @SentinelResource(value = "test2",blockHandler = "handleException")
    public  String test2(){
        userService.getUser();
        return "查询订单";
    }
    @GetMapping("/err")
    @SentinelResource(value = "err",blockHandler = "handleException")
    public  JsonResult err(){
        JsonResult jsonResult=new JsonResult();
        jsonResult.setMessage("testController1");
        int a=1/0;
        return jsonResult;
    }
}
